package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 拉取列表
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        /*
         * 首先从threadlocal中获取是哪个用户添加食品或者套餐到购物车
         * 先判断是食品还是套餐，因为用到的对象不同
         *
         * 再判断购物车中是否已经有这个数据
         * 有则+1，无则添加
         * */
        log.info("购物车数据:{}", shoppingCart);

        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCart.getDishId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        if (dishId == null) {
            objectLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            objectLambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        }

        ShoppingCart shoppingCart1 = shoppingCartService.getOne(objectLambdaQueryWrapper);

        if (shoppingCart1 == null) {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCart1 = shoppingCart;
        } else {
            //如果已经存在，就在原来数量基础上加一
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCart1);
        }

        return R.success(shoppingCart1);

    }

    /**
     * 减少购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        /*
         * 首先从threadlocal中获取是哪个用户添加食品或者套餐到购物车
         * 先判断是食品还是套餐，因为用到的对象不同
         *
         * 购物车中这个数据是否是1
         * 是1则删除，否则-1
         * */
        log.info("购物车数据:{}", shoppingCart);

        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCart.getDishId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        if (dishId == null) {
            objectLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            objectLambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        }

        ShoppingCart shoppingCart1 = shoppingCartService.getOne(objectLambdaQueryWrapper);

        if (shoppingCart1 == null) {
           new CustomException("购物车数据错误");
        } else {
            //在原来数量基础上-1
            Integer number = shoppingCart1.getNumber();
            if(number==1){
                shoppingCartService.remove(objectLambdaQueryWrapper);
            }
            else{
                shoppingCart1.setNumber(number - 1);
                shoppingCartService.updateById(shoppingCart1);
            }

        }

        return R.success(shoppingCart1);

    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }


}